package com.demo.controller;

import com.demo.core.constants.HttpStatus;
import com.demo.core.domain.Result;
import com.demo.core.exception.ParameterException;
import com.demo.domain.LearningResources;
import com.demo.service.LearningResourcesService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/learningResources")
public class LeaningResourcesController {
    @Autowired
    public LearningResourcesService learningResourcesService;

    /**
     * 根据资源类型进行学习资源的搜索
     *
     * @param type
     * @return
     */
    @Operation(summary = "获取对应类型的学习资源")
    @GetMapping("/getAllByType")
    public Result getAllResourcesByType(int type) throws Exception {
        if (type != 0 && type != 1) {
            throw new ParameterException("type值非法");
        }
        List<LearningResources> list = learningResourcesService.getAllResourcesByType(type);
        return list == null || list.size() <= 0 ?
                Result.success(HttpStatus.SUCCESS, "无相关资源", new ArrayList() {}) :
                Result.success(HttpStatus.SUCCESS, "查找成功", list);
    }
}
